package com.fingard.dsp.bank.directbank.lcbip01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBatchOpenBill;
import com.fingard.dsp.bank.reqrespobj.batchbill.*;
import com.fingard.dsp.bank.reqrespobj.batchbill.detail.*;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.io.File;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * Created by F on 2019/10/22.
 */
public class LCBIP01BatchOpenBill extends LCBIP01Base implements IBatchOpenBill {
    private ExecutorService executor = Executors.newFixedThreadPool(1);

    @Override
    public RetAtsBatchQry batchQry(ReqBatchQry query) throws Exception {
        return null;
    }

    @Override
    public RetAtsBatchInfoQry batchInfoQry(ReqBatchInfoQry query) throws Exception {
        return null;
    }

    @Override
    public RetAtsBatchAddSubmit batchAddSubmit(ReqBatchAddSubmit query) throws Exception {
        return null;
    }

    @Override
    public RetAtsBatchModSubmit batchModSubmit(ReqBatchModSubmit query) throws Exception {
        return null;
    }

    @Override
    public RetAtsBatchDelSubmit batchDelSubmit(ReqBatchDelSubmit query) throws Exception {
        return null;
    }

    @Override
    public RetAtsBatchSubmitApply batchSubmitApply(ReqBatchSubmitApply query) throws Exception {
        return null;
    }

    @Override
    public RetAtsBatchSubmitCancel batchSubmitCancel(ReqBatchSubmitCancel query) throws Exception {
        return null;
    }

    @Override
    public RetAtsBillAddSubmit billAddSubmit(ReqBillAddSubmit query) throws Exception {
        return null;
    }

    @Override
    public RetAtsBillAddCancelSubmit billAddCancelSubmit(ReqBillAddCancelSubmit query) throws Exception {
        return null;
    }

    @Override
    public RetAtsBatOpenBillSubmit BatOpenBillSubmit(ReqBatOpenBillSubmit query) throws Exception {
        RetAtsBatOpenBillSubmit retAts = new RetAtsBatOpenBillSubmit(query.reqHeader);
        try {
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            LCBIP01Header header = new LCBIP01Header();
            header.TuxName = "BillApplySubmit";
            header.ReqSeqNo = query.reqBatchNo;
            writeCommonHeader(xmlTextWriter, header);
            xmlTextWriter.writeElementString("ReqReserved1", "", true);
            xmlTextWriter.writeElementString("ReqReserved2", "", true);
            xmlTextWriter.writeElementString("ReqReserved3", "", true);
            xmlTextWriter.writeElementString("ReqReserved4", "", true);
            xmlTextWriter.writeEndElement();
            xmlTextWriter.writeStartElement("body");
            //        CustCode	CHAR	企业编号	Y	集团签约模式：企业直连对接系统内部编号/财务公司编号
            xmlTextWriter.writeElementString("CustCode", bankFront.org);
//        非集团签约模式：财务公司编号
//        OrgName	CHAR	机构名称	Y	默认写死：兵器装备集团财务有限责任公司
            xmlTextWriter.writeElementString("OrgName", "兵器装备集团财务有限责任公司");
//        OrgNo	CHAR	机构号	Y	写死：99
            xmlTextWriter.writeElementString("OrgNo", "99");
//        BillType	CHAR	票据种类	Y	票据种类:
            xmlTextWriter.writeElementString("BillType", query.billType);
//        银行承兑汇票：“1”、
//        商业承兑汇票：“2”
//        OuterBill	CHAR	出票人名称	Y	用户当前企业名称
            xmlTextWriter.writeElementString("OuterBill", query.drawName);
//        BusinessType	CHAR	业务标志	Y	默认写死：传统：12 批售：01
            xmlTextWriter.writeElementString("BusinessType", "12");
//        BillStart	CHAR	合同起始日	Y	合同起始日：yyyyMMdd
            xmlTextWriter.writeElementString("BillStart", query.contractStart.substring(0, 10).replaceAll("-", ""));
//        BillEnd	CHAR	合同到期日	Y	合同到期日：yyyyMMdd
            xmlTextWriter.writeElementString("BillEnd", query.contractEnd.substring(0, 10).replaceAll("-", ""));
//        OpenBillType	CHAR	开票方式	Y	额度开票：1
            xmlTextWriter.writeElementString("OpenBillType", query.openBillType);
//        全额保证金开票:2
//        质押开票:3
//        汽车金融：4
//        其他5
//        IsOccute	CHAR	是否占用授信	Y	是：1
            String creditBreedAmt = query.creditBreedAmt;
            if (StringHelper.hasAnyChar(creditBreedAmt) && Double.valueOf(creditBreedAmt) > 0) {
                xmlTextWriter.writeElementString("IsOccute", "1");
                String creditBreedNo = getBankConfig().getValueAsString("CreditBreedNo");
                xmlTextWriter.writeElementString("CreditBreedNo", creditBreedNo);
                xmlTextWriter.writeElementString("CreditBreedAmt", creditBreedAmt);
            } else {
                xmlTextWriter.writeElementString("IsOccute", "0");
                xmlTextWriter.writeElementString("CreditBreedNo", "");
                //        CreditBreedAmt	CHAR	占用授信金额	Y	不占用为0.00
                xmlTextWriter.writeElementString("CreditBreedAmt", "0.00");
            }
//        MarginAcct	CHAR	保证金账户	Y	企业的保证金账户
            xmlTextWriter.writeElementString("MarginAcct", query.marginAcct);
//        CommissionChargeAcct	CHAR	手续费扣收账户	Y	企业的结算户
            xmlTextWriter.writeElementString("CommissionChargeAcct", query.commissionChargeAcct);
//        Remark	CHAR	备注	N
            xmlTextWriter.writeElementString("Remark", "");
//        OpenApplyNo	CHAR	开票申请内码	Y
            xmlTextWriter.writeElementString("OpenApplyNo", "", true);
//        BillDetailAddReqParams	CHAR[]	票据明细参数上送数组		可多条，可单条，详细信息见票据明细报存-上送信息
            xmlTextWriter.writeStartElement("BillDetailAddReqParams");
            while (query.readRecord()) {
                xmlTextWriter.writeStartElement("BillDetailAddReqParam");
//                PjyfkcNm	CHAR	票据内码	O	如为空时为保存信息 如不为空时为修改信息
                xmlTextWriter.writeElementString("PjyfkcNm", "", true);
//                BillMedium	CHAR	票据介质	M	电票 1 写死
                xmlTextWriter.writeElementString("BillMedium", "1");
//                OuterNo	CHAR	出票人编号	M	用户当前企业编号
                xmlTextWriter.writeElementString("OuterNo", bankFront.org);
//                OuterAcct	CHAR	出票人账号	M	出票人信息 反显客户选择的账户
                xmlTextWriter.writeElementString("OuterAcct", query.drawAct);
//                OuterBankNo	CHAR	出票人开户行行号	M	出票人信息 写死：99
                xmlTextWriter.writeElementString("OuterBankNo", "99");
//                OuterBankName	CHAR	出票人开户行名称	M	出票人信息 默认写死：兵器装备集团财务有限责任公司
                xmlTextWriter.writeElementString("OuterBankName", "兵器装备集团财务有限责任公司");
//                InnerName	CHAR	收票人名称	M	收票人信息
                xmlTextWriter.writeElementString("InnerName", query.detail.payeeName);
//                InnerNo	CHAR	收票人编号	O
                xmlTextWriter.writeElementString("InnerNo", "", true);
//                InnerAcct	CHAR	收票人账号	M	收票人信息
                xmlTextWriter.writeElementString("InnerAcct", query.detail.payeeAct);
//                InnerBankNo	CHAR	收票人开户行行号	M	收票人信息
                xmlTextWriter.writeElementString("InnerBankNo", query.detail.payeeBnkCode);
//                InnerBankName	CHAR	收票人开户行名称	M	收票人信息
                xmlTextWriter.writeElementString("InnerBankName", query.detail.payeeBnkName);
//                BillDate	CHAR	开票日期	M	开票日期：yyyyMMdd
                xmlTextWriter.writeElementString("BillDate", DateHelper.convertD10toD8(query.detail.issDate));
//                EndDate	CHAR	到期日期	M	到期日期：yyyyMMdd
                xmlTextWriter.writeElementString("EndDate", DateHelper.convertD10toD8(query.detail.dueDate));
//                BillAmt	CHAR	票面金额	M
                xmlTextWriter.writeElementString("BillAmt", query.detail.billAmt);
//                AccepterName	CHAR	承兑人名称	M	默认写死：兵器装备集团财务有限责任公司
                xmlTextWriter.writeElementString("AccepterName", "兵器装备集团财务有限责任公司");
//                AccepterNo	CHAR	承兑人编号	M	写死：99
                xmlTextWriter.writeElementString("AccepterNo", "99");
//                AccepterAcct	CHAR	承兑人账户	M	写死：0
                xmlTextWriter.writeElementString("AccepterAcct", "0");
//                IsTrans	CHAR	是否转让	M	可再转让：EM00 不得转让：EM01
                String isTrans;
                if ("1".equals(query.detail.transFlag)) {
                    isTrans = "EM00";
                } else {
                    isTrans = "EM01";
                }
                xmlTextWriter.writeElementString("IsTrans", isTrans);
//                PjyfkcSeqNo	CHAR	票据明细编号
                xmlTextWriter.writeElementString("PjyfkcSeqNo", query.detail.postScript);
                xmlTextWriter.writeEndElement();
            }
            xmlTextWriter.writeEndElement();
//        ReqReserved1 CHAR	备用字段1		 ReqReserved1
            xmlTextWriter.writeElementString("ReqReserved1", "", true);
//                ReqReserved2 CHAR	备用字段2		ReqReserved2
            xmlTextWriter.writeElementString("ReqReserved2", "", true);
//                ReqReserved3 CHAR	备用字段3		ReqReserved3
            xmlTextWriter.writeElementString("ReqReserved3", "", true);
//                ReqReserved4 CHAR	备用字段4		ReqReserved4
            xmlTextWriter.writeElementString("ReqReserved4", "", true);
            xmlTextWriter.writeEndDocument();
            String[] result = sendToBank(xmlTextWriter.toXmlString());
//                String[] result = new String[]{"","<?xml version=\"1.0\" encoding=\"UTF-8\"?><lcbank><head><BankNo>50</BankNo><TuxName>BillApplySubmit</TuxName><ReqSeqNo>RN000015O2</ReqSeqNo><RespCode>1</RespCode><RespDate>20190129</RespDate><RespTime>163345</RespTime><RespInfo>授信额度不足，不能提交</RespInfo><RecordNum>1</RecordNum></head><body><BillApplyAddRespParam><Result>01</Result><PjyfkcNm><String>KP190129006</String></PjyfkcNm><ReqReserved1 /><ReqReserved2 /><ReqReserved3 /><ReqReserved4 /><ErrorInfo>授信额度不足，不能提交</ErrorInfo><BillApplyCode /></BillApplyAddRespParam></body></lcbank>"};
            if (result[0].length() == 0) {
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1], tmpsbMsg);
                if (tmpbkReader != null) {
                    String returnCode = tmpbkReader.getFirstText("/lcbank/head/RespCode");
                    String returnInfo = tmpbkReader.getFirstText("/lcbank/head/RespInfo");
                    if ("0".equals(returnCode)) {
                        retAts.respCode = "0";
                        retAts.respInfo = tmpbkReader.getFirstText("/lcbank/body/BillApplyAddRespParam/ErrorInfo");
                        retAts.transState = tmpbkReader.getFirstText("/lcbank/body/BillApplyAddRespParam/Result");
                        retAts.payInfoCode = retAts.transState;
                        StringBuilder seqData = new StringBuilder();
                        while (tmpbkReader.read("/lcbank/body/BillApplyAddRespParam/Bill")) {
                            RetAtsBatOpenBillSubmitDetail detail = new RetAtsBatOpenBillSubmitDetail();
                            detail.respCode = "0";
                            detail.respInfo = retAts.respInfo;
                            detail.postScript = tmpbkReader.readedXElement.getSingleText("PjyfkcSeqNo");
                            detail.bnkPostScript = tmpbkReader.readedXElement.getSingleText("PjyfkcNm");
                            detail.result = retAts.transState;
                            detail.payInfoCode = retAts.transState;
                            seqData.append(detail.bnkPostScript + "_" + detail.postScript + "\r\n");
                            retAts.writeRetDetail(detail);
                        }
                        String filepath = getBankConfig().getValueAsString("OpenBillSeqNoSaveDir") + File.separator+Format.Date6YearMonth.format(new Date())+File.separator + query.reqBatchNo + ".txt";
                        FileHelper.writeFile(filepath, seqData.toString(), getCharset());
                    } else if ("1".equals(returnCode)) {
                        retAts.respCode = "0";
                        retAts.respInfo = tmpbkReader.getFirstText("/lcbank/body/BillApplyAddRespParam/ErrorInfo");
                        retAts.transState = "1+" + tmpbkReader.getFirstText("/lcbank/body/BillApplyAddRespParam/Result");
                        retAts.payInfoCode = retAts.transState;
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = returnInfo;
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpsbMsg.toString();
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = result[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts = new RetAtsBatOpenBillSubmit(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
        }
        retAts.writeAfterDetail();
        return retAts;
    }

    @Override
    public RetAtsBatOpenBillSubmitQry BatOpenBillSubmitQry(ReqBatOpenBillSubmitQry query) throws Exception {
        RetAtsBatOpenBillSubmitQry retAts = new RetAtsBatOpenBillSubmitQry(query.reqHeader);
        query.readRecord();
        try{
            Map<String,String> sequenceNoMap = new HashMap<String, String>();
            String filename =getBankConfig().getValueAsString("OpenBillSeqNoSaveDir")+ File.separator+Format.Date6YearMonth.format(new Date())+ File.separator+query.detail.reqSeqID +".txt";
            String sequenceNoData = FileHelper.readFileText(filename,getCharset());
            String[] sequenceNoArr = sequenceNoData.split("\\r\\n");
            if(sequenceNoArr==null||sequenceNoArr.length==0){
                throw new Exception("批次序号文件：【"+filename+"】异常，请检查");
            }else {
                List<Future<RetAtsBatOpenBillSubmitQryDetail>> bankRespList = new ArrayList<Future<RetAtsBatOpenBillSubmitQryDetail>>();
                for(String sequenceNoitem :sequenceNoArr){
                    String[] sequenceNoInfo =sequenceNoitem.split("_");
                    sequenceNoMap.put(sequenceNoInfo[0],sequenceNoInfo[1]);
                    ResultQryCallable callable = new ResultQryCallable(query.detail.billType,sequenceNoInfo[0],this);
                    Future<RetAtsBatOpenBillSubmitQryDetail> future = executor.submit(callable);
                    future.get().postScript= sequenceNoInfo[1];
                    bankRespList.add(future);
                }
                for(Future<RetAtsBatOpenBillSubmitQryDetail> future:bankRespList){
                    RetAtsBatOpenBillSubmitQryDetail detail = future.get();
                    if(StringHelper.isNullOrEmpty(retAts.respCode)){
                        retAts.respCode = "0";
                    }
                    retAts.writeRetDetail(detail);
                    sbLog.append(detail.sblog);
                    sbLog.append("\r\n\r\n");
                }
            }
        }catch (Exception e){
            retAts = new RetAtsBatOpenBillSubmitQry(query.reqHeader);
            retAts.respCode ="-1";
            retAts.respInfo = e.getMessage();
            retAts.writeBeforeDetail();
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }


    @Override
    public RetAtsOrgQry orgQry(ReqOrgQry reqOrgQry) throws Exception {
        return null;
    }

    //应付异常查询
    public RetAtsBatOpenBillSubmitQryDetail BillExceptionQueryYf(String billType, String pjyfkcNm) {
        RetAtsBatOpenBillSubmitQryDetail respDetail = new RetAtsBatOpenBillSubmitQryDetail();
        StringBuilder sblog =new StringBuilder();
        try{
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            LCBIP01Header header = new LCBIP01Header();
            header.TuxName = "BillExceptionQueryYf";
            header.ReqSeqNo =getQryTransID();
            writeCommonHeader(xmlTextWriter,header);
            xmlTextWriter.writeElementString("ReqReserved1","",true);
            xmlTextWriter.writeElementString("ReqReserved2","",true);
            xmlTextWriter.writeElementString("ReqReserved3","",true);
            xmlTextWriter.writeElementString("ReqReserved4","",true);
            xmlTextWriter.writeEndElement();
            xmlTextWriter.writeStartElement("body");
//            企业编号	CustCode	Y	集团签约模式：企业直连对接系统内部编号/财务公司编号 非集团签约模式：财务公司编号
            xmlTextWriter.writeElementString("CustCode",bankFront.org);
//            业务类型	Ywlx	Y	出票登记：KP 提示承兑：CD 提示收票：SP 撤票：CP
            xmlTextWriter.writeElementString("Ywlx","CP");
//            操作状态	OpeStatus		允许发送银行：2A
            xmlTextWriter.writeElementString("OpeStatus","");
//            承兑状态	PjyfkcCdzt		承兑-允许发送银行：1A
            xmlTextWriter.writeElementString("PjyfkcCdzt","");
//            提示收票状态	PjyfkcSpzt		提示收票-允许发送银行：1A
            xmlTextWriter.writeElementString("PjyfkcSpzt","");
//            票据编号	PjyfkcPjbh
                xmlTextWriter.writeElementString("PjyfkcPjbh","");
//            撤消状态	PjyfkcDpcxzt
            xmlTextWriter.writeElementString("PjyfkcDpcxzt","");
//            开票起始日期	billingDateS
            xmlTextWriter.writeElementString("billingDateS","");
//            开票结束日期	billingDateE
            xmlTextWriter.writeElementString("billingDateE","");
//            到期日起开始	dateDueS
            xmlTextWriter.writeElementString("dateDueS","");
//            到期日期结束	dateDueE
            xmlTextWriter.writeElementString("dateDueE","");
//            发送日起开始	sendDateS
            xmlTextWriter.writeElementString("sendDateS","");
//            发送日期结束	sendDateE
            xmlTextWriter.writeElementString("sendDateE","");
//            票面金额最小	pjyfkcPmjeS
            xmlTextWriter.writeElementString("pjyfkcPmjeS","");
//            票面金额最大	pjyfkcPmjeE
            xmlTextWriter.writeElementString("pjyfkcPmjeE","");
//            备用字段1	ReqReserved1 reqReserved1
            xmlTextWriter.writeElementString("ReqReserved1",pjyfkcNm);
//            备用字段2	ReqReserved2 reqReserved2
            xmlTextWriter.writeElementString("ReqReserved2","");
//            备用字段3	ReqReserved3 reqReserved3
            xmlTextWriter.writeElementString("ReqReserved3","");
//            备用字段4	ReqReserved4 reqReserved4
            xmlTextWriter.writeElementString("ReqReserved4","");

            xmlTextWriter.writeEndDocument();
            String[] result =sendToBankNoLog(xmlTextWriter.toXmlString(),sblog);
            if(result[0].length()==0) {
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1],tmpsbMsg);//Status
                if(tmpbkReader!=null){
                    String returnCode = tmpbkReader.getFirstText("/lcbank/head/RespCode");
                    String returnInfo = tmpbkReader.getFirstText("/lcbank/head/RespInfo");
                    if("0".equals(returnCode)){
                        if(tmpbkReader.read("/lcbank/body/YfBillExceptionRespParam")) {
                            respDetail.respCode = "0";
                            respDetail.respInfo = returnInfo;
                            respDetail.result = "PjyfkcPjzt+" + tmpbkReader.readedXElement.getSingleText("PjyfkcPjzt");
                            respDetail.payInfoCode = respDetail.result;
                            respDetail.billNo = tmpbkReader.readedXElement.getSingleText("PjyfkcPjbh");
                        } else {
                            respDetail = QueryBillInfor(billType,pjyfkcNm,sblog);
                        }
                    }else {
                        respDetail.respCode="-1";
                        respDetail.respInfo = returnInfo;
                    }
                }else {
                    respDetail.respCode = "-1";
                    respDetail.respInfo = tmpsbMsg.toString();
                }
            }else{
                respDetail.respCode = "-1";
                respDetail.respInfo = result[1];
            }
        }catch (Exception ex){
            ex.printStackTrace();
            respDetail.respCode = "-1";
            respDetail.respInfo =ex.getMessage();
        }
        respDetail.sblog = sblog;
        return respDetail;
    }

    //撤票信息结果查询
    private RetAtsBatOpenBillSubmitQryDetail QueryBillInfor(String billType, String pjyfkcNm, StringBuilder sblog) {
        RetAtsBatOpenBillSubmitQryDetail respDetail = new RetAtsBatOpenBillSubmitQryDetail();
        try{
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            LCBIP01Header header = new LCBIP01Header();
            header.TuxName = "QueryBillInfor";
            header.ReqSeqNo =getQryTransID();
            writeCommonHeader(xmlTextWriter,header);
            xmlTextWriter.writeElementString("ReqReserved1","0",true);
            xmlTextWriter.writeElementString("ReqReserved2","50",true);
            xmlTextWriter.writeElementString("ReqReserved3", "", true);
            xmlTextWriter.writeElementString("ReqReserved4","",true);
            xmlTextWriter.writeEndElement();
            xmlTextWriter.writeStartElement("body");
//            客户编号	CustCode	Y	集团签约模式：企业直连对接系统内部编号/财务公司编号
            xmlTextWriter.writeElementString("CustCode",bankFront.org);
//            查询类型	QueryType	Y	00：持票
            xmlTextWriter.writeElementString("QueryType","01");
//            票据类型	BillType	N	1：银行承兑汇票 2：商业承兑汇票
            xmlTextWriter.writeElementString("BillType",billType);
//            票据形式	Pjxs	N	0: 纸质票据 1: 电子票据
            xmlTextWriter.writeElementString("Pjxs","1");
//            票据编号	BillCode	N
            xmlTextWriter.writeElementString("BillCode", "");
//            开票日期（开始区间）	SDate	N	示例：20180101
            xmlTextWriter.writeElementString("SDate","",true);
//            开票日期（结束区间）	EDate	N	示例：20180631
            xmlTextWriter.writeElementString("EDate","",true);
//            到期日期（开始区间）	EndSDate	N	示例：20180101
            xmlTextWriter.writeElementString("EndSDate","",true);
//            到期日期（结束区间）	EndEDate	N	示例：20180631
            xmlTextWriter.writeElementString("EndEDate","",true);
//            票面金额（开始区间）	SAmt	N
            xmlTextWriter.writeElementString("SAmt","",true);
//            票面金额（结束区间）	EAmt	N
            xmlTextWriter.writeElementString("EAmt","",true);
//           票据合同号	Kpzbnm	N
            xmlTextWriter.writeElementString("Kpzbnm","",true);
//            收款人名称	Skrqc	N
            xmlTextWriter.writeElementString("Skrqc","",true);
//            票据状态	BillState	N	10:开票申请,11:开票申请提交,12:开票申请复核,13:开票申请退回,14:受理拒绝,22:已开票,42:已承付,7:作废,32:到期复核
            xmlTextWriter.writeElementString("BillState","",true);
//            备用字段1	ReqReserved1 N	当前条数
            xmlTextWriter.writeElementString("ReqReserved1","0",true);
//            备用字段2	ReqReserved2 N	每页显示条数，默认显示50条
            xmlTextWriter.writeElementString("ReqReserved2","50",true);
//            备用字段3	ReqReserved3
            xmlTextWriter.writeElementString("ReqReserved3", pjyfkcNm);
//            备用字段4	ReqReserved4
            xmlTextWriter.writeElementString("ReqReserved4","",true);
            xmlTextWriter.writeEndDocument();
            String[] result =sendToBankNoLog(xmlTextWriter.toXmlString(),sblog);
            if(result[0].length()==0) {
                StringBuilder tmpsbMsg = new StringBuilder();
                XmlTextReader tmpbkReader = loadBankRespXmlReader(result[1],tmpsbMsg);//Status
                if(tmpbkReader!=null){
                    String returnCode = tmpbkReader.getFirstText("/lcbank/head/RespCode");
                    String returnInfo = tmpbkReader.getFirstText("/lcbank/head/RespInfo");
                    if("0".equals(returnCode)){
                        if(tmpbkReader.read("/lcbank/body/BillInfoRespParam")) {
                            respDetail.respCode = "0";
                            respDetail.respInfo = returnInfo;
                            respDetail.result = "Pjzt+" + tmpbkReader.readedXElement.getSingleText("Pjzt");
                            respDetail.payInfoCode = respDetail.result;
                            respDetail.billNo =tmpbkReader.readedXElement.getSingleText("Pjbh");
                        }
                    }else {
                        respDetail.respCode="-1";
                        respDetail.respInfo = returnInfo;
                    }
                }else {
                    respDetail.respCode = "-1";
                    respDetail.respInfo = tmpsbMsg.toString();
                }
            }else{
                respDetail.respCode = "-1";
                respDetail.respInfo = result[1];
            }
        }catch (Exception ex){
            ex.printStackTrace();
            respDetail.respCode = "-1";
            respDetail.respInfo =ex.getMessage();
        }
        return respDetail;
    }
}

class ResultQryCallable implements Callable<RetAtsBatOpenBillSubmitQryDetail> {
    public String billType;
    public String pjyfkcNm;
    public LCBIP01BatchOpenBill lcbip01BatchOpenBill;
    public ResultQryCallable(String billType, String pjyfkcNm , LCBIP01BatchOpenBill openBill) {
        this.billType = billType;
        this.pjyfkcNm = pjyfkcNm;
        lcbip01BatchOpenBill = openBill;
    }

    @Override
    public RetAtsBatOpenBillSubmitQryDetail call() throws Exception {
        RetAtsBatOpenBillSubmitQryDetail detail =lcbip01BatchOpenBill.BillExceptionQueryYf(billType,pjyfkcNm);
        return detail;
    }
}
